package jp.co.cos.ittrick.channel.controller.client;

import java.util.List;

import jp.co.cos.ittrick.channel.meta.PreModelMeta;
import jp.co.cos.ittrick.channel.model.PageModel;
import jp.co.cos.ittrick.channel.model.PreModel;
import jp.co.cos.ittrick.channel.service.PageService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class GetPreListController extends Controller {

    private PageService pageService;

    public GetPreListController() {
        pageService = new PageService();
    }

    @Override
    protected Navigation run() throws Exception {

        String page = asString("page");

        PageModel pageModel = pageService.getPageByKey(Integer.valueOf(page));
        List<PreModel> preList = pageModel.getPreModelListRef().getModelList();

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        Object[] entityArray = preList.toArray();
        response.getWriter().println(PreModelMeta.get().modelsToJson(entityArray));
        response.flushBuffer();

        return null;
    }

}
